package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.entity.Accountrecordss;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 * 客户联系记录 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-06-19
 */
@Mapper
public interface AccountrecordssMapper extends BaseMapper<Accountrecordss> {
    @Update("UPDATE dragon_accountrecordss a " +
            "SET a.delete_time = NOW() , a.deleted = CASE a.deleted WHEN 0 THEN 1 end WHERE a.id = #{id}")
    void fakeDelete(Integer id);

    @Update("UPDATE dragon_accountrecordss a SET a.status = CASE a.`status` WHEN 0 THEN 3 END  WHERE " +
            "a.id = #{id}" )
    void auditAccount(Integer id);

    @Delete("DELETE FROM dragon_accountrecordss a WHERE a.customer_id = #{id}")
    void deleteByCustomerId(Integer id);

    @Select("<script>" +
            "SELECT M.*  FROM " +
            "(SELECT a.*,b.`name` AS projectName ,c.customer_name AS customerName,d.username FROM dragon_accountrecordss a " +
            "LEFT JOIN dragon_project b ON a.project_id = b.id " +
            "LEFT JOIN dragon_customer c on a.customer_id = c.id " +
            "LEFT JOIN dragon_admin d on a.record_id = d.id) M " +
            "WHERE 1=1 AND M.deleted = 0 " +
            "<if test='search != null and search != \"\"'> "+
            "AND (M.title like CONCAT('%',#{search},'%')  or M.customerName like CONCAT('%',#{search},'%'))"+
            "</if> " +
            "</script>")
    Page<Accountrecordss> selectAccount(Page<Accountrecordss> aPage, String search);
}
